package com.xc.videosample

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.feo.kpler.FKerVideoPlayerManager
import com.feo.kpler.FKerVideoProgressListener
import com.feo.kpler.HttpMediaCacheServer
import com.xc.videosample.databinding.ActivityAudioBinding

class AudioActivity : AppCompatActivity() {
    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, AudioActivity::class.java))
        }
    }

    lateinit var dataBinding: ActivityAudioBinding
    lateinit var playerManager: FKerVideoPlayerManager
    var playPosition: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val list = ArrayList<String>()

        list.add("https://jiliang-bucket.oss-cn-shanghai.aliyuncs.com/jiliang-bucket/creative/music/XiaoxiaoNeural.mp3")
        list.add("https://jiliang-bucket.oss-cn-shanghai.aliyuncs.com/jiliang-bucket/creative/music/YunxiNeural.mp3")
        list.add("https://jiliang-bucket.oss-cn-shanghai.aliyuncs.com/jiliang-bucket/creative/music/donggan.m4a")
        list.add("https://jiliang-bucket.oss-cn-shanghai.aliyuncs.com/jiliang-bucket/creative/music/XiaoyiNeural.mp3")
        list.add("https://jiliang-bucket.oss-cn-shanghai.aliyuncs.com/jiliang-bucket/creative/music/XiaochenNeural.mp3")

        dataBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_audio)

        playerManager = FKerVideoPlayerManager(dataBinding.playerView)


//        playerManager.start(list[playPosition])
        HttpMediaCacheServer.cacheVideo(list[playPosition])
        dataBinding.btnStart.setOnClickListener {
            playerManager.activeResume()
        }

        dataBinding.btnPause.setOnClickListener {
            playerManager.activePause()
        }

        dataBinding.btnReplay.setOnClickListener {
            playerManager.start(list[playPosition])
        }
        dataBinding.btnNext.setOnClickListener {
            playPosition++

            if (playPosition >= list.size) {
                playPosition = 0
            }
            playerManager.start(list[playPosition])
        }
        playerManager.registerProgressListener(object : FKerVideoProgressListener {
            override fun start() {
            }

            override fun onProgress(currentPosition: Int, duration: Int) {
                Log.i("chyy", "onProgress currention: $currentPosition  durtion: $duration")
            }

            override fun complete() {
            }

        })
    }

    override fun onResume() {
        super.onResume()
        playerManager.resume()
    }

    override fun onPause() {
        super.onPause()
        playerManager.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        playerManager.destroy()
    }
}